package com.wangzhen.codebase.paocha;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

// 烧水泡茶的demo演示
// 1、洗水壶-- 2、烧茶-- 5、泡茶
// 3、洗茶壶-- 4、拿茶叶
public class PaoChaV1 {

    // 1、洗水壶-- 2、烧茶-- 5、泡茶
    static class Step1 implements Callable<String> {
        private final FutureTask task;
        public Step1(FutureTask task) {
            this.task = task;
        }

        @Override
        public String call() throws Exception {
            System.out.printf("threadName=%s-->1、洗水壶\n", Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(1);
            System.out.printf("threadName=%s-->2、烧开水\n", Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(30);

            // 等待另外一个线程把茶壶、茶叶都弄好了
            task.get();
            System.out.printf("threadName=%s-->3、喝茶吧\n", Thread.currentThread().getName());
            return "";
        }
    }

    // 3、洗茶壶-- 4、拿茶叶
    static class Step2 implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.printf("threadName=%s-->洗茶壶\n", Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(1);
            System.out.printf("threadName=%s-->拿茶叶\n", Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(1);
            return "洗完茶壶，也拿完茶叶了！";
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> task1 = new FutureTask<>(new Step2());
        FutureTask<String> task2 = new FutureTask<>(new Step1(task1));


        Thread t1 = new Thread(task1);
        Thread t2 = new Thread(task2);

        t1.start();t2.start();
        task2.get();

    }




}
